import 'dart:io';

import 'package:cookie_jar/cookie_jar.dart';
import 'package:dio/dio.dart';
import 'package:dio_cookie_manager/dio_cookie_manager.dart';

class NetUtil {
  static NetUtil _instanse;
  static Dio _dio;

  static Map<String, dynamic> optHeader = {
    'accept-language': 'zh-cn',
    'content-type': 'application/json'
//    'content-type': 'application/x-www-form-urlencoded'
  };

  static NetUtil getIns() {
    if (_instanse == null) {
      _instanse = NetUtil._();
    }
    if (_dio == null) {
      _dio = Dio(
        BaseOptions(
          connectTimeout: 10000,
          headers: optHeader,
          baseUrl: "https://www.wanandroid.com",
          contentType: ContentType.json.toString(),
          responseType: ResponseType.json,
        ),
      );
//      _dio.interceptors.add(CookieManager(PersistCookieJar()));
    }
    return _instanse;
  }

  NetUtil._();

  Future get(String url, Map<String, dynamic> params) async {
    var response;
    response = await _dio.get(url, queryParameters: params);
    return response.data;
  }

  Future post(String url, Map<String, dynamic> params) async {
    var response;
    response = await _dio.post(url, queryParameters: params);
    return response.data;
  }
}
